package com.lagou.edu.controller;

import com.lagou.edu.pojo.Token;
import com.lagou.edu.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/code/token")
public class TokenController {

    @Autowired
    private TokenService tokenService;

    @RequestMapping("/save")
    public Long save(@RequestBody Token token) {
        if (StringUtils.isEmpty(token.getEmail())
            || StringUtils.isEmpty(token.getToken())) {
            return 0L;
        }
        Long id = tokenService.save(token);
        return id;
    }

    @RequestMapping("/get/token")
    public String getByEmail(@RequestBody Token token) {
        if (StringUtils.isEmpty(token.getEmail())) {
            return "";
        }
        Token unique = tokenService.getUnique(token);
        if (unique == null) {
            return "";
        } else {
            return unique.getToken();
        }
    }

    @RequestMapping("/get/email")
    public String getByToken(@RequestBody Token token) {
        if (StringUtils.isEmpty(token.getToken())) {
            return "";
        }
        Token unique = tokenService.getUnique(token);
        if (unique == null) {
            return "";
        } else {
            return unique.getEmail();
        }
    }


}
